package com.sevenstudio.adu.gasing.object;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.modifier.ScaleAtModifier;

import android.os.Handler;

import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.define.iServer_Data;
import com.sevenstudio.adu.gasing.element.AScene;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Utils;
import com.sevenstudio.adu.gasing.game.element.GameElementSprite;
import com.sevenstudio.adu.gasing.state.State_Game_Play;

public class Item {

	Item_ELement[] spr_item = null;
	private int lengthArr;
	int indexRandom = -1;
	int item;
	AScene scene;
	GameEngine engine;
	private boolean useItem;
	private boolean hit;
	private long timer;
	
	public Item(GameEngine engine, AScene scene, GameElementSprite[] Arr_spr, int lengthArr)
	{
		this.scene = scene;
		this.engine = engine;
		spr_item = new Item_ELement[lengthArr];
		this.lengthArr = lengthArr;
		
		for(int i= 0; i < lengthArr; i++)
		{
			spr_item[i] = new Item_ELement(engine, Arr_spr, i);
		}
		useItem = false;
		hit = false;
		//		for(int i=0; i < iGameDefines.SPRITE_EFFECT_ITEM.length; i++)
//		{
//			State_Game_Play.mc.getSprite().attachChild(State_Game_Play.mc.item_effect[i].theSprite());
//			State_Game_Play.enemy.getSprite().attachChild(State_Game_Play.enemy.item_effect[i].theSprite());
//			
//			State_Game_Play.mc.item_effect[i].theSprite().setVisible(false);
//			State_Game_Play.enemy.item_effect[i].theSprite().setVisible(false);
//		}
	}
	
	public void onUpdate()
	{
				
		switch (item) 
		{
		case 0:
			float indX;
			float indY;
			indexRandom = getIndexItem();
			indX = Utils.GetRandomInRange(150, 650);
			indY = Utils.GetRandomInRange(70, 410);
			
			if(!useItem)
			{
			spr_item[indexRandom].getSprite().theSprite().setPosition(Utils.GetRatioWidth(indX), Utils.GetRatioHeight(indY));
			engine.SceneAttach(scene, spr_item[indexRandom].getSprite());
			item++;
			}
			break;

		case 1:
			
			
			if(System.currentTimeMillis() % 200 == 0)
			{
				
				item=2;
			}
			else
			{
				
				spr_item[indexRandom].sprite.theSprite().registerEntityModifier(new ScaleAtModifier(1f, 1.8f, 1, 0, 0));
			}
			
			break;
		case 2:
			spr_item[indexRandom].getSprite().DetachSelf();
			item =0;
			break;
		
		}
		
		itemCek();
	}
	
	void itemCek()
	{
		
		
		switch (indexRandom) {
		case iGameDefines.ITEM_FREZE:
			
		if(!hit)
		{
			if(spr_item[indexRandom].getSprite().theSprite() != null)
			{
				if(spr_item[indexRandom].getSprite().theSprite().collidesWith(State_Game_Play.mc.getSprite())
						&& spr_item[indexRandom].getSprite().theSprite().getScaleX() == 1f)
					{
		//				Aksi untuk item 0
						State_Game_Play.mc.setMove(false);
						State_Game_Play.mc.setPlay(false);
						useItem = true;
						hit = true;
						
						if(!State_Game_Play.mc.item_effect[1].theSprite().isVisible())
						State_Game_Play.mc.item_effect[0].theSprite().setVisible(true);
						
						
						spr_item[indexRandom].getSprite().DetachSelf();
					}
					
					else if(spr_item[indexRandom].getSprite().theSprite().collidesWith(State_Game_Play.enemy.getSprite())
							&& spr_item[indexRandom].getSprite().theSprite().getScaleX() == 1f)
					{
						
						State_Game_Play.enemy.setPlay(false);
						
						if(!State_Game_Play.enemy.item_effect[1].theSprite().isVisible())
						State_Game_Play.enemy.item_effect[0].theSprite().setVisible(true);
						
						useItem = true;
						hit = true;
						
						spr_item[indexRandom].getSprite().DetachSelf();
					}
			}
//			Utils.TRACE("Kena Kau");
		}
		else
		{
			if(useItem)
			{
		
				
//				State_Game_Play.scene.theScene().registerUpdateHandler(new TimerHandler(1f, new ITimerCallback() {
//					
//					public void onTimePassed(TimerHandler pTimerHandler) {
//						// TODO Auto-generated method stub
//
//							useItem = false;
//						
//					}
//				}));
				
				if(System.currentTimeMillis() % 100 == 0)
				{
					useItem = false;
				}
			}
			else
			{
				
				if(!State_Game_Play.mc.isMove())
				{
					State_Game_Play.mc.setMove(true);
					State_Game_Play.mc.item_effect[0].theSprite().setVisible(false);
				}
				if(!State_Game_Play.enemy.isPlay)
				{
					State_Game_Play.enemy.setPlay(true);
					State_Game_Play.enemy.item_effect[0].theSprite().setVisible(false);
				}
				hit = false;
				Utils.TRACE("ITEM KENA");
			}
		}	
			break;

		case iGameDefines.ITEM_POWER:
			if(!hit)
			{
			
				if(spr_item[indexRandom].getSprite().theSprite().collidesWith(State_Game_Play.mc.getSprite())
						&& spr_item[indexRandom].getSprite().theSprite().getScaleX() == 1f)
				{
	//				Aksi untuk item 1
					spr_item[indexRandom].getSprite().DetachSelf();
					useItem = true;
					
					iServer_Data.doubleAttack = 2;
					
				
					if(!State_Game_Play.mc.item_effect[0].theSprite().isVisible())
						State_Game_Play.mc.item_effect[1].theSprite().setVisible(true);
					
					hit = true;
					
				}
				else if(spr_item[indexRandom].getSprite().theSprite().collidesWith(State_Game_Play.enemy.getSprite())
						&& spr_item[indexRandom].getSprite().theSprite().getScaleX() == 1f
						)
				{
					spr_item[indexRandom].getSprite().DetachSelf();
					useItem = true;
					
					iServer_Data.doubleAttack = 2;
					
					if(!State_Game_Play.enemy.item_effect[0].theSprite().isVisible())
					State_Game_Play.enemy.item_effect[1].theSprite().setVisible(true);
					
					hit = true;
					
				}
			}
			else
			{
				if(useItem)
				{
//					Utils.TRACE("ITEM ");
//					State_Game_Play.scene.theScene().registerUpdateHandler(new TimerHandler(2f, new ITimerCallback() {
//						
//						public void onTimePassed(TimerHandler pTimerHandler) {
//							// TODO Auto-generated method stub
//						
//					
//								useItem = false;
//							
//						}
//					}));
					
					if(System.currentTimeMillis() % 100 == 0)
					{
						useItem = false;
						iServer_Data.doubleAttack = 1;
					}
				}
				else
				{
					
					if(!State_Game_Play.mc.isMove())
					{
//						State_Game_Play.mc.setMove(true);
						State_Game_Play.mc.item_effect[1].theSprite().setVisible(false);
					}
					if(!State_Game_Play.enemy.isPlay)
					{
//						State_Game_Play.enemy.setPlay(true);
						State_Game_Play.enemy.item_effect[1].theSprite().setVisible(false);
					}
					hit = false;
				}
			}
			
			break;
		case iGameDefines.ITEM_HEALT:
			
				if(spr_item[indexRandom].getSprite().theSprite().collidesWith(State_Game_Play.mc.getSprite()))
				{
	//				Aksi untuk item 2
					if(!useItem)
					{
						useItem = true;
						spr_item[indexRandom].getSprite().DetachSelf();
						if(State_Game_Play.barHealth[0].theSprite().getHeight() < Utils.GetRatio(iGameDefines.SPRITE_GAME[iGameDefines.SPRITE_GAME_BAR_HEALTH_LEFT].getH()));
						State_Game_Play.barHealth[0].theSprite().setHeight(State_Game_Play.barHealth[0].theSprite().getHeight() + Utils.GetRatio(16));
						
						
					}
					
					
				}
				else if(spr_item[indexRandom].getSprite().theSprite().collidesWith(State_Game_Play.enemy.getSprite()))
				{
					if(!useItem)
					{
						useItem = true;
						
						spr_item[indexRandom].getSprite().DetachSelf();
					
						if(State_Game_Play.barHealth[1].theSprite().getHeight() < Utils.GetRatio(iGameDefines.SPRITE_GAME[iGameDefines.SPRITE_GAME_BAR_HEALTH_RIGHT].getH()));
						State_Game_Play.barHealth[1].theSprite().setHeight(State_Game_Play.barHealth[1].theSprite().getHeight() + Utils.GetRatio(16));
					
						
					}
				}
			
		
			break;
		}
		
	}

	int getIndexItem()
	{
		return Utils.GetRandomInRange(0, lengthArr - 1);
		
	}

	void setItem(int item)
	{
		this.item = item;

	}
	
	public void resetItem()
	{
		useItem = false;
		hit =false;
	}
	
	
}
